<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>lagou manage</title>
<meta name="description" content="拉勾数据">
<meta name="keywords" content="二手房 房产经济 房企 开发商 拉勾数据 IFS">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="icon" type="image/png" href="assets/i/favicon.png">
<link rel="apple-touch-icon-precomposed"
	href="assets/i/app-icon72x72@2x.png">
<meta name="apple-mobile-web-app-title" content="eosms for IFS" />
<link rel="stylesheet" href="assets/css/amazeui.min.css" />
<link rel="stylesheet" href="assets/css/admin.css">



</head>
<body>
	<!--[if lte IE 9]>
<p class="browsehappy">你正在使用<strong>过时</strong>的浏览器，Amaze UI 暂不支持。 请 <a href="http://browsehappy.com/" target="_blank">升级浏览器</a>
  以获得更好的体验！</p>
<![endif]-->

	<jsp:include page="/manage_header.do" flush="true" />

	<div class="am-cf admin-main">
		<!-- sidebar start -->
		<jsp:include page="/manage_tree.do" flush="true" ></jsp:include>
		<!-- sidebar end -->

		<!-- content start -->
		<div class="admin-content">

			<div class="am-cf am-padding">
				<div class="am-fl am-cf">
					<strong class="am-text-primary am-text-lg">小区</strong> / <small>community</small>
				</div>
			</div>
			<form id="date_form" class="am-form" action="show_add_community.do"
				method="post" data-am-validator>
				<input type="hidden" name="id" id="id" value="0"> <input
					type="hidden" name="page_num" id="page_num" value="0">

				<div class="am-tabs am-margin" data-am-tabs>
					<ul class="am-tabs-nav am-nav am-nav-tabs">
						<li class="am-active"><a href="#tab1">基本信息</a></li>
					</ul>
					<div class="am-tabs-bd">
						<div class="am-tab-panel am-fade am-in am-active" id="tab1">
							<div class="am-g am-margin-top">
								<div class="am-u-sm-2 am-u-lg-2 am-text-right">
									<select class="selector1" id="province" name="province"></select>
								</div>
								<div class="am-u-sm-2 am-u-lg-2 am-text-right">
									<select class="selector2" id="city" name="city"></select>
								</div>
								<div class="am-u-sm-2 am-u-lg-2 am-text-right">
									<select class="selector3" id="county" name="county"></select>
								</div>
								<div class="am-u-sm-1 am-u-lg-1 am-text-right">小区</div>
								<div class="am-u-sm-2 am-u-lg-2">
									<input type="text" class="am-input-sm" name="name" id="name"
										value="${name}">
								</div>
								<div id="div_status">
									<div class="am-u-sm-1 am-u-md-1 am-text-right">状态</div>
									<div class="am-u-sm-2 am-u-md-2 am-text-right">
										<select class="selector1" id="status" name="status">
											<option value="">请选择状态</option>
											<option value="1">可用</option>
											<option value="0">禁用</option>
										</select>

									</div>
								</div>
							</div>
							<br>
							<div class="am-margin am-text-right">
							<button type="button" id="clear_button"
									class="am-btn am-btn-primary am-btn-xs">清空</button>
								<button type="button" id="search_button"
									class="am-btn am-btn-primary am-btn-xs">查询</button>
								<button type="button" id="save_button"
									class="am-btn am-btn-primary am-btn-xs">修改</button>
								<button type="button" id="add_button"
									class="am-btn am-btn-primary am-btn-xs">新增</button>
								
									<button type="button" id="delete_button"
								class="am-btn am-btn-primary am-btn-xs">删除</button>
							</div>
						</div>
					</div>
					</div>
			</form>
			<div class="am-g">
				<div class="am-u-sm-12">
					<form class="am-form">
						<table id="clients_table"
							class="am-table am-table-striped am-table-hover table-main">
							<thead>
								<tr>
									<th class="table-title" width="20%">省</th>
									<th class="table-title" width="10%">市</th>
									<th class="table-title" width="20%">县/区</th>
									<th class="table-title" width="20%">小区</th>
									<th class="table-title" width="10%">状态</th>
								</tr>
							</thead>
							<tbody id="data_tbody">
								<c:forEach var="community" items="${communities}" varStatus="s">
									<tr class="tr_click" id="${ community.id }"
										community_id="${community.id}">
										<td id="td_provincename_${community.id}">${community.province.name}</td>
										<td id="td_cityname_${community.id}">${community.city.name}</td>
										<td id="td_countyname_${community.id}">${community.county.name}</td>
										<td id="td_communityname_${community.id}">${community.name}</td>
										<td class="td1"><c:choose>
												<c:when test="${community.status==1}">  
         											  可用     
   												</c:when>
												<c:otherwise> 
													   禁用 
  											    </c:otherwise>
											</c:choose>
											</td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
						<div class="am-cf">
							共 <b id="pageCount">${page.allRow}</b> 条记录
							<div class="am-fr">
								<ul class="am-pagination">
									<c:choose>
										<c:when test="${page.currentPage==1}">
											<li class="am-disabled"><a href="#">«</a></li>
										</c:when>
										<c:when test="${page.currentPage!=1}">
											<li><a href="javascript:void(0)"
												onclick="choose_page(${page.currentPage-1})">«</a></li>
										</c:when>
									</c:choose>
									<c:forEach var="i" begin="1" end="${page.totalPage}" step="1">
										<c:choose>
											<c:when test="${page.currentPage==i}">
												<li class="am-active"><a href="javascript:void(0)"><c:out
															value="${i}" /></a></li>
											</c:when>
											<c:when test="${page.currentPage!=i}">
												<li><a href="javascript:void(0)"
													onclick="choose_page(${i})"><c:out value="${i}" /></a></li>
											</c:when>
										</c:choose>
									</c:forEach>
									<c:choose>
										<c:when test="${page.currentPage==page.totalPage}">
											<li class="am-disabled"><a href="#">»</a></li>
										</c:when>
										<c:when test="${page.currentPage!=page.totalPage}">
											<li><a href="javascript:void(0)"
												onclick="choose_page(${page.currentPage+1})">»</a></li>
										</c:when>
									</c:choose>
								</ul>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>

		<!-- content end -->

	</div>

	<a href="#" class="am-show-sm-only admin-menu"
		data-am-offcanvas="{target: '#admin-offcanvas'}"> <span
		class="am-icon-btn am-icon-th-list"></span>
	</a>

	<jsp:include page="/manage_foot.do" flush="true" />

	<!--[if lt IE 9]>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/modernizr/2.8.3/modernizr.js"></script>
<script src="assets/js/amazeui.ie8polyfill.min.js"></script>
<![endif]-->

	<!--[if (gte IE 9)|!(IE)]><!-->
	<script src="assets/js/jquery.min.js"></script>
	<!--<![endif]-->
	<script src="assets/js/amazeui.min.js"></script>
	<script src="assets/js/app.js"></script>
	<script type="text/javascript">

			//省
			$(function() {	
					$.ajax({
					contentType:"application/json",
				    type : "POST",
				    url : "<%=request.getContextPath()%>/get_province.do", 
				    dataType: "json",
				    success : function(data) {
				        if(data.flag=="1"){
				    	var length=data.area.length;
				       if(length>0){
		   		         var selDom=$("#province");
		   		   		 selDom.html("");
		   		   	     selDom.append("<option value=''>请选择省</option>");
				    	 for(var i=0;i<length;i++){
				    		 selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");
				    		 }  
				    	 
				    	 var selDom1=$("#city");
		   		   	     selDom1.append("<option value=''>请选择市</option>");
		   		         var selDom2=$("#county");
	   		   	         selDom2.append("<option value=''>请选择县</option>");
				    	
				    	 
				    	 }}
				       }
				});
				
				$("#province").change(function() {
					$("#province>option:selected").each(function(index){
						if(index == $("#province>option:selected").length-1){							
							var code=$(this).val();
							 console.log(code);
					$.ajax({
						contentType:"application/json",
					    type : "POST",
					    url : "<%=request.getContextPath()%>/get_city/"+code+"/.do",
					    dataType: "json",
					    success : function(data) {
					        if(data.flag=="1"){
				
					    	var length=data.area.length;
					       if(length>0){
			   		         var selDom=$("#city");
			   		           selDom.html("");
			   		   	 selDom.append("<option value=''>请选择市</option>");
			   		     
					    	 for(var i=0;i<length;i++){
					    		 selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");
					    		 }  
					    	 }
					       }
					       }
					});
				}
			});
		});
						//市
			$(function() {	
			$("#city").change(function() {
				$("#city>option:selected").each(function(index){
					if(index == $("#city>option:selected").length-1){							
						$("#county").empty();
						var code=$(this).val();
						 $.ajax({
								contentType:"application/json",
							    type : "POST",
							    url : "<%=request.getContextPath()%>/get_county/"+code+"/.do", 
							    dataType: "json",
							    success : function(data) {
							        if(data.flag=="1"){
							    	var length=data.area.length;
							    	console.log(length);
							       if(length>0){
					   		         var selDom=$("#county");
					   		      selDom.html("");
						   		   	 selDom.append("<option value=''>请选择县</option>");

							    	 for(var i=0;i<length;i++){
							    		 
							    		 selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");
							    		 }  
							    	 }}
							       }
							});
					}
				});
				
			});
			});				
$(function() {	
	//页面初始化时默认新增菜单，所以隐藏新增按钮
	$("#save_button").hide();//隐藏
	$("#delete_button").hide();
	 $("#div_status").hide();
	var pageSize = ${pageSize};
	//保存按钮点击事件
	$("#save_button").click(function() {		
		var saveData={"id":$("#id").val(),"name":$("#name").val()};
		$.ajax({
			contentType:"application/json",
		    type : "POST",
		    url : "<%=request.getContextPath()%>/add_community.do?province_code="+$("#province>option:selected").val()+"&city_code="+$("#city>option:selected").val()+"&county_code="+$("#county>option:selected").val()+"&status="+$("#status>option:selected").val()+"",
		    data : JSON.stringify(saveData),
		    dataType: "json",
		    success : function(data) {
		        if(data.flag=="1"){
		        	$("#id").val(data.id);
		        	  $("#name").val("");
		        	  $("#province").val("");
		        	  $("#city").val("");
		        	  $("#county").val("");
		        	  $("#status").val("");
			    	 $("#date_form").submit();
		            alert("保存成功！");
		        }
		        else{
		            alert("保存失败！");
		        }
		    },
		    error :function(){
		        alert("网络连接出错！");
		    }
		});
	});
});
	 
	 //菜单列表点击事件
	 $(".tr_click").click(function(){
		 $("#div_status").show();
		 $.ajax({
				contentType:"application/json",
			    type : "GET",
			    url : "<%=request.getContextPath()%>/get_community/"+$(this).attr("community_id")+"/.do",
			    dataType: "json",
			    success : function(data) {
			        if(data.flag=="1"){
			        	  var status=data.community.status;
			        	$("#id").val(data.community.id);
			        	$("#name").val(data.community.name);
			        	 if(status==1)
		            	   {
		            	   $("#status option[value='1']").attr("selected","selected");
		            	   $("#status option[value='0']").removeAttr("selected");
		            	   }
		               if(status==0)
		            	   {
		            	   $("#status option[value='0']").attr("selected","selected");
		            	   $("#status option[value='1']").removeAttr("selected");
		            	   }
			        	var province_code = data.community.province.code;
			        	var city_code = data.community.city.code;
			        	var county_code = data.community.county.code;
			        	//重新加载省，并且选中
				   		 $.ajax({
								contentType:"application/json",
							    type : "POST",
							    url : "<%=request.getContextPath()%>/get_province.do", 
							    dataType: "json",
						    	success : function(data) {
							        if(data.flag=="1"){
								    	var length=data.area.length;
								        if(length>0){
							   		        var selDom=$("#province");
							   		   		selDom.html("");
										    for(var i=0;i<length;i++){										    	
										    	if(data.area[i].code == province_code){
										    		selDom.append("<option value='"+data.area[i].code+"' selectes='selected'>"+data.area[i].name+"</option>");	

										    	}else{
										    		selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");
										    	}
										    } 
							    		}
							        }
						       }
						});
			        	
			        	//重新加载对应的市，并选中
			        	$.ajax({
							contentType:"application/json",
						    type : "POST",
						    url : "<%=request.getContextPath()%>/get_city/"+province_code+"/.do",
						    dataType: "json",
						    success : function(data) {
						        if(data.flag=="1"){				
							    	var length=data.area.length;
							       	if(length>0){
						   		   		var selDom=$("#city");
						   		        selDom.html("");			   		     
								    	for(var i=0;i<length;i++){
								    		if(data.area[i].code == city_code){
									    		selDom.append("<option value='"+data.area[i].code+"' selected='selected'>"+data.area[i].name+"</option>"); 
								    		}else{
									    		selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");
								    		}
								    	}  
							      	}
						       }
						    }
						});
			        
			        	//重新加载对应的县，并选中
			        	$.ajax({
							contentType:"application/json",
							type : "POST",
							url : "<%=request.getContextPath()%>/get_county/"+city_code+"/.do", 
							dataType: "json",
							success : function(data) {
								if(data.flag=="1"){
							    	var length=data.area.length;
							    	console.log(length);
							        if(length>0){
					   		        	var selDom=$("#county");
					   		      		selDom.html("");
							    	 	for(var i=0;i<length;i++){
							    			if(data.area[i].code == county_code){
							    				selDom.append("<option value='"+data.area[i].code+"' selected='selected'>"+data.area[i].name+"</option>");									    		 
							    			}else{
							    				selDom.append("<option value='"+data.area[i].code+"'>"+data.area[i].name+"</option>");		
							    			}
							    		 }  
							    	 }}
							       }
						});

			      }
			        else{
			            alert("获取菜单失败！");
			        }
			    },
			    error :function(){
			        alert("网络连接出错！");
			    }
			});
		 $(".tr_click").removeClass("am-active");
		 $(this).addClass("am-active");
         $("#save_button").show();//显示
         $("#delete_button").show();
	 });
	 //删除按钮
	 	 $("#delete_button").click(function(){
		
		 $.ajax({
				contentType:"application/json",
			    type : "GET",
			    url : "<%=request.getContextPath()%>/delete_community.do?id="+$("#id").val()+"",
			    dataType: "json",
			    success : function(data) {
			        if(data.flog==1){
			        	 alert("删除成功！");
			        	 $("#id").val(0);
			    		 $('#date_form')[0].reset();
			    		 $("#date_form").submit();
			        }
			        else{
			            alert("删除失败！");
			        }
			    },
			    error :function(){
			        alert("网络连接出错！");
			    }
			});
		 $(".tr_click").removeClass("am-active");
		 $(this).addClass("am-active");
         $("#save_button").show();//显示
	 });
	 
	 //清空按钮点击事件
	 $("#clear_button").click(function(){
		 $(".tr_click").removeClass("am-active");
		 $("#id").val(0);
		 $("#name").val('');
		  $("#province").val("");
    	  $("#city").val("");
    	  $("#county").val("");
    	  $("#status").val("");
         $("#save_button").hide();//隐藏
	 });
	 
	//新增按钮点击事件
	 $("#add_button").click(function(){
		 $("#id").val(0);
		 $("#save_button").click();
		 $(".tr_click").removeClass("am-active");
	 });
	
	//查询按钮点击事件
	 $("#search_button").click(function(){
		 $("#date_form").submit();
	 });
});
function choose_page(i){
	/*  $("#clear_button").click(); */
	$("#page_num").val(i);

	$("#date_form").submit();
	}

</script>
</body>
</html>